package ltd.dujiabao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.commonutils.Result;
import ltd.dujiabao.eduservice.entity.EduCourse;
import ltd.dujiabao.eduservice.entity.vo.CourseInfoForm;
import ltd.dujiabao.eduservice.entity.vo.CoursePublishVo;
import ltd.dujiabao.eduservice.entity.vo.CourseQuery;
import ltd.dujiabao.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author dujiabao
 * @since 2021-04-21
 */
@Api(value = "课程管理")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/course")
@Slf4j
public class EduCourseController {

  @Autowired
  EduCourseService eduCourseService;

  @ApiOperation("提交课程表单")
  @PostMapping
  public Result addCourse(@ApiParam(name = "courseInfoForm", value = "课程表单", required = true) @RequestBody CourseInfoForm courseInfoForm) {
    boolean isSuccess = eduCourseService.saveCourseInfo(courseInfoForm);
    if (isSuccess) {
      return Result.success().addData("id", courseInfoForm.getId());
    } else {
      return Result.failure();
    }
  }

  @ApiOperation("更新课程表单")
  @PutMapping("/{id}")
  public Result updateCourse(@ApiParam(name = "id", required = true) @PathVariable("id") String id,
                             @ApiParam(name = "courseInfoForm", value = "课程表单", required = true) @RequestBody CourseInfoForm courseInfoForm) {
    courseInfoForm.setId(id);
    boolean isSuccess = eduCourseService.updateCourseInfo(courseInfoForm);
    if (isSuccess) {
      log.info("更新成功，id 为" + id + "的课程");
      return Result.success().addData("id", id);
    } else {
      log.error("更新失败，id为" + id + "的课程");
      return Result.failure();
    }
  }

  @ApiOperation("查询课程表单")
  @GetMapping("/{id}")
  public Result getCourse(@ApiParam(name = "id", required = true) @PathVariable("id") String id) {
    CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(id);
    return Result.success().addData("data", courseInfoForm);
  }


  @ApiOperation("根据条件筛选获取课程列表")
  @PostMapping("/coursesCondition/{current}/{size}")
  public Result getCoursesCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                                    @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size,
                                    @ApiParam(name = "courseQuery", value = "查询条件")
                                    @RequestBody(required = false) CourseQuery courseQuery) {
    Page<EduCourse> page = new Page<>(current, size);
    eduCourseService.pageQuery(page, courseQuery);
    List<EduCourse> courses = page.getRecords();
    long pages = page.getPages();
    long total = page.getTotal();
    return Result.success().addData("data", courses).addData("pages", pages).addData("total", total)
            .addData("current", current).addData("size", size);
  }

  @ApiOperation("获取课程发布确认信息的对象")
  @GetMapping("/getPublish/{id}")
  public Result getCoursePublish(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
    CoursePublishVo coursePublishVo = eduCourseService.getCoursePublish(id);
    return Result.success().addData("data", coursePublishVo);
  }

  @ApiOperation("正式发布课程")
  @GetMapping("/publish/{id}")
  public Result publishCourse(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
    eduCourseService.publishCourse(id, true);
    log.info("发布" + id + "课程成功");
    return Result.success();
  }

  @ApiOperation("删除课程")
  @DeleteMapping("/{id}")
  public Result removeCourse(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
    boolean isSuccess = eduCourseService.removeCourse(id);
    if (isSuccess) {
      log.info("删除" + id + "课程成功");
      return Result.success();
    } else {
      log.error("删除" + id + "课程失败");
      return Result.failure();
    }
  }

}

